#!/usr/bin/env python

import sys
import gtk


_builders = {}


class GuiObjectNotFound(object):
    pass


class GuiFileNotFound(object):
    pass


def load(filename):
    global _builders
    if filename not in _builders:
        _builders[filename] = gtk.Builder()
        _builders[filename].add_from_file(filename)


def connect(controller, filename=None):
    global _builders

    if filename != None:
        if filename not in _builders:
            load(filename)

        _builders[filename].connect_signals(controller)

    elif len(_builders) == 1:
        _builders.values()[0].connect_signals(controller)

    else:
        raise GuiFileNotFound


def get_object(name, filename=None):
    global _builders

    window = None
    if filename != None:
        window = _builders[filename].get_object(name)
    else:
        window = _builders.values()[0].get_object(name)
    
    if window == None:
        raise GuiObjectNotFound

    return window
